﻿using System;
using System.Collections.Generic;
using System.Windows.Markup;

namespace Com.Lancetinc.Evs.Helpers
{
    /// <summary>
    /// 本地化标记扩展 
    /// </summary>
    [MarkupExtensionReturnType(typeof(string))]
    public class LocalizeExtension : MarkupExtension
    {
        public LocalizeExtension()
        {
        }

        public LocalizeExtension(string key)
        {
            this.Key = key ?? throw new ArgumentNullException("key");
        }

        public LocalizeExtension(string key, bool isLabel)
        {
            this.Key = key ?? throw new ArgumentNullException("key");
            this.IsLabel = isLabel;
        }
        /// <summary>
        ///  获取或设置此标记扩展所要取得本地化字符串的键。
        /// </summary>
        [ConstructorArgument("key")]
        public string Key { get; set; }

        [ConstructorArgument("isLabel")] public bool IsLabel { get; set; } = false;

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (string.IsNullOrEmpty(this.Key))
                throw new InvalidOperationException("Key cannot be empty.");

            var msg = I18nHelper.Default.GetMessage(this.Key);
            if (IsLabel)
                msg += I18nHelper.LabelStr;
            return msg;
        }
    }

    [MarkupExtensionReturnType(typeof(string))]
    public class LocWatermarkExtension : MarkupExtension
    {
        public LocWatermarkExtension()
        {
        }

        public LocWatermarkExtension(string key)
        {
            this.Key = key;
        }

        public LocWatermarkExtension(string key, int type)
        {
            this.Key = key;
            this.Type = type;
        }

        /// <summary>
        ///  获取或设置此标记扩展所要取得本地化字符串的键。
        /// </summary>
        [ConstructorArgument("key")]
        public string Key { get; set; }

        /// <summary>
        /// 0:文本框
        /// 1:选择框
        /// </summary>
        [ConstructorArgument("type")]
        public int Type { get; set; } = 0;

        private static readonly Dictionary<int, string> WatermarkFmtDic = new();

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (!WatermarkFmtDic.TryGetValue(Type, out var watermarkFmt))
            {
                watermarkFmt = I18nHelper.Default.GetMessage("PlzInput" + Type);
                WatermarkFmtDic[Type] = watermarkFmt;
            }

            string msg = "";
            if (!string.IsNullOrEmpty(this.Key))
                msg = I18nHelper.Default.GetMessage(this.Key);

            return string.Format(watermarkFmt, msg);
        }
    }

}